<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Photo_manag extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('file');
        $this->load->helper('form');
        $this->load->database();
        $this->load->helper('func');
        $this->load->model('photos_process');
        $this->load->model('alblum_process');
    }

    public function index() {
        $data["album_array"] = $this->alblum_process->album_type_array_admin(1); //0为照片类型
        $ablum_id = get_segment_fuc(4);
        if (isset($ablum_id)) {
            $data["photo_array"] = $this->photos_process->photo_list($ablum_id);
        } else {
            if (!empty($data["album_array"])) {
                $data["photo_array"] = $this->photos_process->photo_list($data["album_array"][0]["id"]);
            } else {
                $data["photo_array"] = array();
            }
        }
        //print_r($data);

        $this->load->view('admin/header_view');
        $this->load->view('admin/photos_view', $data);
        $this->load->view('admin/footer_view');
    }

    public function del_photo() {
        $id = get_segment_fuc(4);
        if (isset($id)) {
            $sqldel = "delete from photos where id=\"{$id}\"";
            $this->db->query($sqldel);
        }
        redirect("admin/photo_manag");
    }

    public function del_album() {
        $id = get_segment_fuc(4);
        if (isset($id)) {
            $sql_is_photo="select * from photos where album_id=\"{$id}\"";
                 $res=$this->db->query( $sql_is_photo);
                 if(!empty($res)){
           echo "<script language='javascript'>alert(\"分类下包含内容请先删除具体记录\")</script>";
           redirect("admin/photo_manag");
                 }
            $sqldel = "delete from album where id=\"{$id}\"";
            $this->db->query($sqldel);
            $sqldel = "delete from photos where album_id=\"{$id}\"";
            $this->db->query($sqldel);
        }
        redirect("admin/photo_manag");
    }

    //编辑相册分类的封面
    public function edit_photoAlbum() {

        if (isset($_POST["edit_submit"]) && is_numeric($_POST["id"])) {
            $this->alblum_process->update_album($_POST["id"]);

            redirect("admin/photo_manag");
        }
        $id = get_segment_fuc(4);
        if (isset($id)) {
            $sqldel = "select * from album where id=\"{$id}\"";
            $p = $this->db->query($sqldel);
            $data['row'] = $p->row_array();
        }
        $data["edit_type"] = 'photoAlbum';
        $this->load->view('admin/edit_view', $data);
    }

    public function edit_photo_bat() {//批量修改照片字段
     //   print_r($_POST);
        if(!isset($_POST["photo_id"] )){
             redirect("admin/photo_manag");
            
        }
        foreach ($_POST["photo_id"] as $key => $val) {
            $post_var_title = "title{$val}";
            $post_var_album_id = "album_id{$val}";
            $post_var_desc = "desc{$val}";
            $post_var_order = "order{$val}";
 $update_sql = "update photos set title=\"{$_POST[$post_var_title]}\" ,album_id=\"{$_POST[$post_var_album_id]}\",`desc`=\"{$_POST[$post_var_desc]}\",`order`=\"{$_POST[$post_var_order]}\" where id=\"{$val}\"";
$this->db->query($update_sql);

        }
             redirect("admin/photo_manag");
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */